/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.server.redisservice;

import cn.ac.iie.di.wx.server.common.RuntimeEnv;
import static cn.ac.iie.di.wx.server.common.RuntimeEnv.redisVos;
import static cn.ac.iie.di.wx.server.redisservice.RedisThread.logger;
import cn.ac.iie.di.wx.server.utils.JedisConnectionHandler;
import cn.ac.iie.di.wx.server.vo.RedisVo;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;

/**
 *
 * @author Administrator
 */
public class JedisThread implements Runnable {

    public LinkedBlockingQueue<RedisVo> redisVos;
    private static final Logger logger = Logger.getLogger(JedisThread.class.getName());

    public JedisThread(LinkedBlockingQueue<RedisVo> redisVos) {
        this.redisVos = redisVos;
    }

    @Override
    public void run() {
        Jedis jedis = null;
        JedisConnectionHandler jedisConn = null;
        try {

//            String[] ipAndPort = ((String) RuntimeEnv.getParam(RuntimeEnv.redis_hosts)).split(":");
            jedisConn = new JedisConnectionHandler((String) RuntimeEnv.getParam(RuntimeEnv.redis_hosts),
                    (String) RuntimeEnv.getParam(RuntimeEnv.redis_masterName),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.redis_dbName)));

//            logger.info(jedis);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        while (true) {
            try {
                jedis = jedisConn.getJedis();
                RedisVo redisVo = redisVos.poll(1, TimeUnit.SECONDS);
                if (redisVo != null) {
                    String flag = jedis.setex(redisVo.getKey(), 60 * 60 * 24 * 15, redisVo.getValue());
                    if (flag.equals("OK")) {
                        String value = jedis.get(redisVo.getKey());
                        logger.info("set into redis is " + flag + ",opid is " + redisVo.getKey() + ",value is " + redisVo.getValue());
                        if (redisVo.getValue().equals(value)) {
                            logger.info("set and get key = " + redisVo.getKey() + ";value = " + value);
                        } else {
                            logger.warn("set and get failed,key = " + redisVo.getKey() + ";value = " + value);
                        }
                    } else {
                        RuntimeEnv.retryQueue.put(new RedisVo(redisVo.getKey(), redisVo.getValue()));
                        logger.info("set " + redisVo.getKey() + " redis " + flag + ",will retry");
                    }

                    Thread.sleep(10);
                }

            } catch (InterruptedException ex) {
                logger.error(ex.getMessage(), ex);
            } finally {
                if (jedis != null) {
                    jedisConn.returnResource(jedis);
                }
            }
        }
    }

}
